package com.lin.mr.maizilaundry.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lin.mr.maizilaundry.R;
import com.lin.mr.maizilaundry.fragmentpay.FragmentAlipay;
import com.lin.mr.maizilaundry.fragmentpay.FragmentRemain;
import com.lin.mr.maizilaundry.myadapter.ClothAdapter;

import java.util.ArrayList;

public class PayActivity extends FragmentActivity {
    private ImageView iv_back_pay;
    private TextView tv_remain;
    private TextView tv_aliPay;
    private ViewPager pay_viewpager;
    private ArrayList<Fragment> fragmentPay;
    public static String amount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay);
        initView();
        initViewPager();
        //接收从BaseAdapter中传递过来的金额
        Intent intent = getIntent();
        this.amount = intent.getStringExtra("amount");

    }

    private void initView() {
        iv_back_pay = (ImageView) findViewById(R.id.iv_back_pay);
        tv_remain = (TextView) findViewById(R.id.tv_remain);
        tv_aliPay = (TextView) findViewById(R.id.tv_aliPay);
        pay_viewpager = (ViewPager) findViewById(R.id.pay_viewpager);
        //返回按钮
        iv_back_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_remain.setOnClickListener(new MyListener(0));
        tv_aliPay.setOnClickListener(new MyListener(1));
    }

    class MyListener implements View.OnClickListener{
        private int index = 0;

        public MyListener(int index) {
            this.index = index;
        }

        @Override
        public void onClick(View v) {
            pay_viewpager.setCurrentItem(index);
        }
    }

    private void initViewPager() {
        pay_viewpager = (ViewPager) findViewById(R.id.pay_viewpager);
        fragmentPay = new ArrayList<>();
        FragmentRemain remain = new FragmentRemain();
        FragmentAlipay aliPay = new FragmentAlipay();
        fragmentPay.add(remain);
        fragmentPay.add(aliPay);
        pay_viewpager.setAdapter(new ClothAdapter(getSupportFragmentManager(),fragmentPay));
        pay_viewpager.setCurrentItem(0);//设置初始页
        pay_viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setTextColor(position);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void setTextColor(int i){
        if (i==0){
            tv_remain.setTextColor(getResources().getColor(R.color.color_white));
            tv_remain.setBackgroundColor(getResources().getColor(R.color.color_light_orange));
            tv_aliPay.setTextColor(getResources().getColor(R.color.color_second_text));
            tv_aliPay.setBackgroundColor(getResources().getColor(R.color.color_white));
        }else if (i==1){
            tv_aliPay.setTextColor(getResources().getColor(R.color.color_white));
            tv_aliPay.setBackgroundColor(getResources().getColor(R.color.color_light_orange));
            tv_remain.setTextColor(getResources().getColor(R.color.color_second_text));
            tv_remain.setBackgroundColor(getResources().getColor(R.color.color_white));
        }
    }

}
